// ininput  边框颜色
let num = $("input").length
for (let i = 0; i < num; i++) {
    $("input").eq(i).click(function() {
        $("input").css({ border: "1px solid #DCE1E4" })
        $(this).css({ border: "1.5px solid #90BAFE" })
    })
}
// 
let boolname = false
let boolpwd = false
let password;
let regu = /^1[3|4|5|7|8]\d{9}$/
let regp = /^[0-9]{6,18}$/
let v1, v2, v3
$("input").eq(0).blur(function() {
        let v = $("input").eq(0).val()
        if (v == "") {
            $(".flavr-fixer").css({ display: "block" })
            $(".flavr-content p").html("请输入手机号码")
        } else if (regu.test(v) == false) {
            $(".flavr-outer").css({ display: "block" })
            $(".flavr-content p").html("手机格式不正确")
        } else if (regu.test(v)) {
            boolname = true
            v1 = v
        }
        $(".flavr-button").click(function() {
            $(".flavr-outer").css({ display: "none" })
        })
    })
    // 密码
$("input").eq(1).blur(function() {
    let v = $("input").eq(1).val()
        // console.log(v);
    if (v == "") {
        $(".flavr-outer").css({ display: "block" })
        $(".flavr-content p").html("密码不能为空")
    } else if (v.length < 6) {
        $(".flavr-outer").css({ display: "block" })
        $(".flavr-content p").html("密码长度不能小于6位")
    } else if (regp.test(v)) {
        boolpwd = true
        v2 = v
    }
    $(".flavr-button").click(function() {
        $(".flavr-outer").css({ display: "none" })
    })
})

// ajax
$("#login-btn").click(function() {
    console.log(v1, v2);
    if (boolname && boolpwd) {
        let xhr;
        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHttp");
        } else {
            xhr = new XMLHttpRequest();
        }
        // console.log(1)
        // 测试
        xhr.open("POST", "./js/login.php", true);
        // java
        // xhr.open("post","checkUserServlet",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.onreadystatechange = function() {

                if (xhr.status == 200 && xhr.readyState == 4) {
                    console.log(xhr.responseText)
                    fun(xhr.responseText);
                    // console.log((xhr.responseText))
                }
            }
            //4.
            //b.在send中添加参数
            //key1=value1&key2=value2
            // console.log(v,v2);
        xhr.send("username=" + v1 + "&password=" + v2);

        function fun(str) {
            // let oSpan = document.querySelector("span");

            if (str != 1) {
                alert("用户已存在,请重新登录");
            } else if (str == 1) {
                alert("登录成功;点击跳转到首页");
                window.location.href = "index.html";
            }
        }
    }
})